package com.sls.web.manager.game.service.impl;

import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.Goods;
import com.sls.web.manager.game.querybean.GoodsQuery;
import com.sls.web.manager.game.service.GoodsService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

@Repository("goodsService")
public class GoodsServiceImpl extends SuperService implements GoodsService {
	private static Logger log = Logger.getLogger(GoodsServiceImpl.class);
	private Gson gson;

	public GoodsServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean add(Goods goods, UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter GoodsService add method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_ADD);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		goods.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, goods);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave GoodsService add method...");
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean update(Goods goods, UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter GoodsService update method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_UPDATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		goods.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, goods);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave GoodsService update method...");
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean updateState(String id, String goodsState, String goodsId,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter GoodsSerice updateState method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_UPDATE_STATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		Goods goods = new Goods();
		goods.setId(id);
		goods.setGoodsId(goodsId);
		goods.setGoodsState(goodsState);
		goods.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, goods);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave GoodsSerice updateState method...");
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<Goods> queryGoods(GoodsQuery goodsQuery,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter GoodsService queryGoods method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_PAGE_QUERY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, goodsQuery);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			Pagination<Goods> goodsBrand = gson.fromJson(body,
					new TypeToken<Pagination<Goods>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave GoodsService queryGoods method...");
			return goodsBrand;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public Goods queryGoodsById(String id, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter GoodsService queryGoodsBrandById method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_QUERY_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		Goods goods = new Goods();
		goods.setGoodsId(id);
		SocketDTO dto = super.send(head, goods);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			Goods goods2 = gson.fromJson(dto.getBody(), Goods.class);
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave GoodsService queryGoodsBrandById method...");
			return goods2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void deleteByGoodsId(String goodsId, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021009");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		Goods goods = new Goods();
		goods.setGoodsId(goodsId);
		SocketDTO dto = super.send(head, goods);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void saveGoodsSupply(Goods goods, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021011");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		goods.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, goods);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void setPrice(Goods goods, UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021030");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		goods.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, goods);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

}
